package com.example.book_system.common;

import java.util.Calendar;
import java.util.Date;

/** 该类用于给出一些常用的方法
 * @author BlueRose
 */
public class Methods {


    /**
     * 返回格式化的时间串 YY-MM-DD hh-mm-ss
     * @return 时间串
     */
    public static String getTimeNow(){
        StringBuilder nowTime = new StringBuilder("");
        Calendar calendar = Calendar.getInstance();
        nowTime.append(calendar.get(Calendar.YEAR)).append("-");
        int nine = 9;
        if(calendar.get(Calendar.MONTH) < nine){
            nowTime.append("0");
        }
        nowTime.append(calendar.get(Calendar.MONTH) + 1).append("-");
        if(calendar.get(Calendar.DAY_OF_MONTH) < nine + 1){
            nowTime.append("0");
        }
        nowTime.append(calendar.get(Calendar.DAY_OF_MONTH)).append(" ");
        if(calendar.get(Calendar.HOUR_OF_DAY) < nine + 1){
            nowTime.append("0");
        }
        nowTime.append(calendar.get(Calendar.HOUR_OF_DAY)).append(":");
        if(calendar.get(Calendar.MINUTE) < nine + 1){
            nowTime.append("0");
        }
        nowTime.append(calendar.get(Calendar.MINUTE)).append(":");
        if(calendar.get(Calendar.SECOND) < nine + 1){
            nowTime.append("0");
        }
        nowTime.append(calendar.get(Calendar.SECOND));
        return nowTime.toString();
    }

    public static long getCmTime(String timeStr){
        // ****-**-** **:**:**
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, new Integer(timeStr.substring(0, 4)));
        calendar.set(Calendar.MONTH, (new Integer(timeStr.substring(5, 7)) - 1));
        calendar.set(Calendar.DATE, new Integer(timeStr.substring(8, 10)));
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        // 注意要除 1000 因为得到的是毫秒字符串
        return calendar.getTime().getTime() / 1000L;
    }
}
